// pages/info_supply/info_supply.js
const app = getApp();
const {
  mobile_login,
  mobileCode,
  getPhoneNumber,
  editName
} = require('../../config');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    validate: '',
    type: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.type == 2) {
      this.setData({
        mobile: options.mobile,
        validate: options.validate,
        type: options.type,
      })
    } else if(options.type == 0) {
      this.setData({
        mobile: options.mobile || '',
        type: options.type,
      })
    } else if (options.type == 3) {
      this.setData({
        type: options.type,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  formSubmit: function(e) {
    let name = e.detail.value.name;
    console.log(22, name.replace(/\s+/g, ''))
    if (!name || name.replace(/\s+/g, '') == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    let openid = app.globalData.openid;
    let userInfo = wx.getStorageSync('userInfo');
    let unionid = wx.getStorageSync('unionid');
    const {
      mobile,
      validate,
      type
    } = this.data
    
    if (type==2){
      wx.request({
        url: mobile_login,
        data: {
          mobile: mobile,
          validate: validate,
          type: 2,
          name: name,
          avatar: userInfo.avatarUrl,
          openid: openid,
          unionid: unionid,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
        method: 'POST',
        success: (res) => {
          if (res.data.code == 0) {
            let token = res.data.data.token;
            app.globalData.token = token;
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        }
      })
    } else if(type==0) {
      wx.request({
        url: mobile_login,
        data: {
          mobile: mobile,
          openid: openid,
          type: 0,
          name: name,
          avatar: userInfo.avatarUrl,
          unionid: unionid
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
        method: 'POST',
        success: (res) => {
          if (res.data.code == 0) {
            let token = res.data.data.token;
            app.globalData.token = token;
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        }
      })
    } else if(type==3){
      app.appRequest({
        url: editName,
        method: 'POST',
        data:{
          name: name,
        },
        success: (res)=>{
          if(res.code==0){
            wx.navigateBack({
              delta: 2
            });
          }
        }
      })
    }
  }
})